/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Linq;
using System.Windows.Controls;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif
namespace Oceanside
{
    /// <summary>
    /// 
    /// </summary>
    public partial class PageDayPartImp : PageImpBaseClass
    {
        DbLinkedTextBox tbEndTimeTextBox;
        DbLinkedTextBox tbStartTimeTextBox;
        DbLinkedTextBox tbTextBoxCompleteName;
        ComboBox comboBoxName;

        /// <summary>
        /// Constructor
        /// </summary>
        public PageDayPartImp(UIElementCollection stackPanControlsChild, 
            DbLinkedTextBox tbStartTimeTextBox, 
            DbLinkedTextBox tbEndTimeTextBox,
            DbLinkedTextBox tbTextBoxCompleteName,
            DbLinkedTextBox textBoxDaypartOridinal, 
            ComboBox comboBoxName, 
            Button bSelectDate, 
            GroupBox elementGroupBox)
        {
            bSelectDate.Click += new System.Windows.RoutedEventHandler(bSelectDate_Click);

            this.ButtonNumber = "13";
            this.ButtonLabelText = "Dayparts";

            mNewGroupIsDisabled = true;
            mDeleteGroupIsDisabled = true;
            mButtonNewEntityName = "New Day Part";
            mButtonDeleteEntityName = "Delete Day Part";
            RegisterSaveMeGroupBoxes(null, elementGroupBox);

            this.tbEndTimeTextBox = tbEndTimeTextBox;
            this.tbStartTimeTextBox = tbStartTimeTextBox;
            this.tbTextBoxCompleteName = tbTextBoxCompleteName;
            this.comboBoxName = comboBoxName;

            ///Register the controls and lock the form.
            RegisterUiElementsList(stackPanControlsChild);

            ///Add all text boxes to the list.
            mTextBoxesList.Add(tbEndTimeTextBox);
            mTextBoxesList.Add(tbStartTimeTextBox);
            mTextBoxesList.Add(textBoxDaypartOridinal);
            mTextBoxesList.Add(tbTextBoxCompleteName);


            ///Add the comboboxes.
            ListofComboboxes.Add(comboBoxName);
            mListOfControlsToHideInNonEditMode.Add(bSelectDate);

        }


        void bSelectDate_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                DAYPART selected = comboBoxName.SelectedItem as DAYPART;
                if (selected != null)
                {
                    TimePicker dtp = new TimePicker(true);
                    dtp.ShowDialog();
                    if (TimePicker.LastResponse == UserResponse.YES)
                    {
                        selected.StartTime = TimePicker.LastStartSelectedDateTime;
                        selected.EndTime = TimePicker.LastEndSelectedDateTime;
                        Context.SaveChanges();
                    }
                }
            }
            catch (Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_REPORT_ERROR, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        override public List<EntityObject> GetEntityObjectSet()
        {
            try
            {
                var q = from DP in Context.DAYPARTS where DP.IsDeleted == false select DP;
                if (q.Any())
                {
                    return new List<EntityObject>(q.OrderBy(dp=>dp.DaypartOrdinal).ToList());
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override EntityObject GetSelectedEntityObject()
        {
            DAYPART selected = comboBoxName.SelectedItem as DAYPART;
            return selected;
        }

        /// <summary>
        /// 
        /// </summary>
        public override void MarkEntityDeleted()
        {
            try
            {
                DAYPART selected = comboBoxName.SelectedItem as DAYPART;
                if (selected != null)
                {
                    selected.IsDeleted = true;
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        public override void UpdateUsersEntitySource()
        {
            try
            {
                ///Create the query object.
                var query = from DAYPART in Context.DAYPARTS
                            where DAYPART.IsDeleted == false
                            orderby DAYPART.DaypartOrdinal
                            select DAYPART;

                if (query.Any())
                {
                    List<DAYPART> list = query.ToList();
                    comboBoxName.ItemsSource = list;
                    if (list != null && list.Count != 0)
                    {
                        comboBoxName.SelectedItem = list.First();
                    }
                    else
                    {
                        comboBoxName.ItemsSource = null;
                    }
                }
                else
                {
                    comboBoxName.ItemsSource = null;
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override EntityObject AddNewEntityObject()
        {
            DAYPART newObject = null;
            try
            {
                var q = from DAYPART in Context.DAYPARTS where DAYPART.Name.Equals("Not Assigned") select DAYPART;

                if (!q.Any())
                {
                    newObject = new DAYPART();
                    newObject.Name = "Not Assigned";
                    newObject.StartTime = DateTime.Now;
                    newObject.EndTime = DateTime.Now;
                    newObject.DaypartOrdinal = 0;
                    Context.DAYPARTS.AddObject(newObject);
                }
                else
                {
                    newObject = q.First();
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
            return newObject;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ComboBox GetEntityComboBox()
        {
            return comboBoxName;
        }
    }
}
